package com.icss.servlets;
/*
 * 登录验证
 * 管志春
 * 2010.03.31
 * 
 * */
import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.icss.beans.bbsUserVO;
import com.icss.daos.bbsUserDao;

public class bbsLoginDealServlet extends HttpServlet {

	/**
	 * Constructor of the object.
	 */
	public bbsLoginDealServlet() {
		super();
	}

	/**
	 * The doGet method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		response.setContentType("text/html");
		PrintWriter out = response.getWriter();
		doPost(request, response);
	}

	/**
	 * The doPost method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to post.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		PrintWriter out = response.getWriter();
		
		
		String param = request.getParameter("param");
		
		String userName = request.getParameter("userName");
		String password = request.getParameter("password");
		response.setContentType("text/html");
		
		bbsUserDao userdao = new bbsUserDao();
		boolean flag =false;
		
		
		//登录请求
		if(param.equals("login")){
		
		bbsUserVO user = userdao.bbsLogin(userName, password);
		
		if(user!=null){
			
			int bid = user.getBid();
			int pid = user.getPid();
			System.out.println(pid);
			HttpSession session = request.getSession();
			session.setAttribute("userName", userName);     //session存值
			session.setAttribute("bid", bid);				//session存值
			session.setAttribute("pid", pid);				//session存值
			//System.out.println(session.getAttribute("pid"));
			request.getRequestDispatcher("/BBS/mainBBS.jsp").forward(request, response);
		}else{
			
			out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
			out.println("<HTML>");
			out.println("  <HEAD><TITLE>系统登录</TITLE></HEAD>");
			out.println("  <BODY>");
			out.print("  登录失败！！<a href='javascript:history.go(-1)'>返回</a>");
			
			out.println("  </BODY>");
			out.println("</HTML>");
			out.flush();
			out.close();
		}
	}
	
		//注册请求
		if(param.equals("register")){
			
			if(null!=userName && ""!=userName && ""!=password && null!=password){
				
				
				String sql = "insert into BBSUser(pid,userName,userPwd)values(default,'"+userName+"','"+password+"')";
				flag = userdao.executeSQL(sql);
				
				if(flag){
					
					out.write("<script>alert('注册成功');location.href='/ICSS/BBS/mainBBS.jsp'</script>");
					
				}else{
					out.write("<script>alert('注册失败');location.href='javascript:history.go(-1)'</script>");
			
					}
				
				out.close();

				
			}else{
				
				out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
				out.println("<HTML>");
				out.println("  <HEAD><TITLE>系统注册</TITLE></HEAD>");
				out.println("  <BODY>");
				out.print("  注册失败！！<a href='javascript:history.go(-1)'>返回</a>");
				
				out.println("  </BODY>");
				out.println("</HTML>");
				out.flush();
				out.close();
			}
			
		}
		
		//注销请求
		if(param.equals("logout")){
			
			HttpSession session = request.getSession();
			Object name = session.getAttribute("userName");
			if("null".equals(name)){
				out.write("<script>alert('你还没有登录！！！');location.href('javascript.history.go(-1)')</script>");	
				out.close();
			}else{
				session.removeAttribute("userName");		//删除session中的值 
				session.removeAttribute("pid");				//删除session中的值 
				session.removeAttribute("bid");				//删除session中的值 
				request.getRequestDispatcher("/BBS/mainBBS.jsp").forward(request,response);
			}
		}
		
	}
}
